/**
 * @Author: wangjian
 * 656876071@qq.com
 * @Date: 2024-07-23 15:52:41
 * @LastEditTime: 2024-09-09 23:16:18
 * @FilePath: src/core/api/sys/FileStorageApi.ts
 * @Description:
 */

import r from '@/core/utils/request'
import type {UploadFile} from "ant-design-vue";
const prefix = '/file/storage'
export const fileStorageApi = () => {
    const uploadFile = (file: UploadFile) => {
        let formData = new FormData()
        if(!file){
            throw new Error('文件不能为空')
        }
        formData.append('file', file)
        return r.post(`${prefix}/upload-post`,formData,{
            transformRequest: [function(data, headers) {
                // 去除post请求默认的Content-Type
                delete headers['Content-Type']
                return data
            }],
        })
    }
    
    // 获取直链
    const getStraightChain = (id: string) => {
        return r.get(`${prefix}//get-straight-chain/${id}`)
    }
    
    // 批量获取直链
    const getStraightChainBatch = (ids: string) => {
        return r.get(`${prefix}/get-straight-chain-batch`, {params: {ids}})
    }
    
    // 预览地址
    const previewAddr = () => {
        return r.get(`${prefix}/preview-addr`)
    }
    
    return {
        uploadFile,
        previewAddr,
        getStraightChain,
        getStraightChainBatch
    }
}
